GetDiscountsBySchoolQueryHandler.execute   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 19
rs 9.65
c 0
b 0
f 0
cc 2
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { GetDiscountsBySchoolQuery } from './GetDiscountsBySchoolQuery';
4
import { IDiscountRepository } from 'src/Domain/School/Repository/IDiscountRepository';
5
import { DiscountView } from '../../View/DiscountView';
6
7
@QueryHandler(GetDiscountsBySchoolQuery)
8
export class GetDiscountsBySchoolQueryHandler {
9
  constructor(
10
    @Inject('IDiscountRepository')
11
    private readonly discountRepository: IDiscountRepository
12
  ) {}
13
14
  public async execute({ schoolId }: GetDiscountsBySchoolQuery): Promise<DiscountView[]> {
15
    const discountViews: DiscountView[] = [];
16
    const discounts = await this.discountRepository.findBySchool(
17
      schoolId
18
    );
19
20
    for (const discount of discounts) {
21
      discountViews.push(
22
        new DiscountView(
23
          discount.getId(),
24
          discount.getType(),
25
          discount.getAmount() / 100,
26
          discount.getValue() / 100
27
        )
28
      );
29
    }
30
31
    return discountViews;
32
  }
33
}
34